/* 
 * CMPS 258: Programming Languages
 *        Assignment 2 - Part2: "Spy" - March 24, 2008
 *        Group name: Doomsday
 *        Authors:  Ramsey Nasser (ID# 200600496) 
 *                  Hadi Abu Rislan (ID# 200600426) 
 *                  Kevin Azzam (ID# 200602033)
 * 
 * Utitlity macros and boolean type definition.
 * 
 * These macros make safely allocating memory a lot easier, and make the code
 * they are used in more readable.
 */
#ifndef UTIL_H_
#define UTIL_H_

typedef char BOOL;

#define TRUE	1
#define FALSE	0

extern int errexit(const char *format, ...);

// Check to make sure memory was allocated to a pointer, die if it wasnt
#define check_mem(pointer)				if(!pointer) errexit("Out of memory");

// Safely allocate memory to a pointer
#define allocate(pointer,size)			pointer = (typeof(pointer))malloc(size);check_mem(pointer)

// Grow an array pointed to by pointer with size oldsize by one element
#define allocateg(pointer,oldsize)		oldsize++;pointer = (typeof(pointer))realloc(pointer,sizeof(*pointer) * oldsize);check_mem(pointer);

// Allocate memory to a pointer equal to the size required by what is it pointing to
#define allocatep(pointer)				allocate(pointer,sizeof(typeof(*pointer)))

// Write memory to a buffer, incrementing the buffer
#define memwrite(dest, src, size)		memcpy(dest, src, size);dest+=size;
// Set a buffer to a character, incrementing the buffer
#define memwritec(dest, c, size)		memset(dest, c, size);dest+=size;

#define memread(from, to, size)			memcpy(to,from,size);from+=size;

#endif /*UTIL_H_*/
